package com.smarttest.portal.function.paramsfunction;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.testng.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.smarttest.common.util.InterfaceUtils;

public class ParamsFunction {
	//用来存放报文、MQ消息等发送时间的map,通过key获取发送的报文map，再通过该报文map的key获取第几条发送的报文时间
	protected static Map<String, Map<Integer, Date>> msg_map = new HashMap<String, Map<Integer, Date>>();
	public synchronized static void setMsg_map(Map<String, Map<Integer, Date>> msg_map) {
		ParamsFunction.msg_map = msg_map;
	}

	public static Map<String, Map<Integer, Date>> getMsg_map() {
		return msg_map;
	}
	private static String trip_starttime = nowBeforeOrAfter("yyyy-MM-dd HH:mm:ss", "-2", String.valueOf(Calendar.HOUR));
	/**
	 * 取当前时间
	 * @param timestamp 时间格式
	 * @return
	 */
	public static String now(String timestamp) {
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(timestamp);
		return sdf.format(d);
	}

	/**
	 * 返回当前时间前/后多少秒/天/年/月等
	 * 
	 * @param timestamp
	 *            返回时间格式
	 * @param interval
	 *            时间间隔
	 * @param calendarType
	 *            calender的时间类型 如Calender.Minute
	 * @return
	 */
	public static String nowBeforeOrAfter(String timestamp, String interval,
			String calendarType) {
		SimpleDateFormat sdf = new SimpleDateFormat(timestamp);
		Calendar calender = Calendar.getInstance();
		calender.add(Integer.parseInt(calendarType), Integer.parseInt(interval));
		return sdf.format(calender.getTime());
	}

	/**
	 * 返回某时间前/后多少秒/天/年/月等
	 * @param time 
	 * @param timestamp
	 *            返回时间格式
	 * @param interval
	 *            时间间隔
	 * @param calendarType
	 *            calender的时间类型 如Calender.Minute
	 * @return
	 * @throws ParseException 
	 */
	public static String getTimeBeforeOrAfter(String time, String timestamp, String interval,
			String calendarType) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat(timestamp);
		Calendar calender = Calendar.getInstance();
		calender.setTime(sdf.parse(time));
		calender.add(Integer.parseInt(calendarType), Integer.parseInt(interval));
		return sdf.format(calender.getTime());
	}
	/**
	 * 获得昨天的0点
	 * 
	 * @return String
	 */
	public static String yesterDayStart() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		String sd = sdf.format(new Date(new Date().getTime() - 24 * 60 * 60
				* 1000));
		return sd;
	}

	/**
	 * 获得昨天的23点59粉59秒
	 * 
	 * @return String
	 */
	public static String yesterDayEnd() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
		String sd = sdf.format(new Date(new Date().getTime() - 24 * 60 * 60
				* 1000));
		return sd;
	}

	/**
	 * 获得当前时间
	 * 
	 * @return String
	 */
	public static String now() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sd = sdf.format(new Date(new Date().getTime()));
		return sd;
	}

	/**
	 * 获得当天的0点0粉0秒
	 * 
	 * @return String
	 */
	public static String todayStart() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		String sd = sdf.format(new Date(new Date().getTime()));
		return sd;
	}

	/**
	 * 获得第二天的0点0粉0秒
	 * 
	 * @return String
	 */
	public static String todayEnd() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
		String sd = sdf.format(new Date(new Date().getTime()));
		return sd;
	}

	/**
	 * 获得当前时间的前几天
	 * 
	 * @param lastInt
	 *            前几天
	 * @return String
	 */
	public static String nowBeforeDays(String lastInt) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sd = sdf.format(new Date(new Date().getTime() - Integer.parseInt(lastInt) * 24
				* 60 * 60 * 1000));
		return sd;
	}

	/**
	 * 获得当前时间的后几天
	 * 
	 * @param afterInt
	 *            后几天
	 * @return String
	 */
	public static String nowAfterDays(String afterInt) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sd = sdf.format(new Date(new Date().getTime() + Integer.parseInt(afterInt) * 24
				* 60 * 60 * 1000));
		return sd;
	}
	
	
	/**
	 * 获得报文发送时间
	 * 
	 * @param msgtype
	 *            获取报文类型(设置到容器时的key)
	 * @param index
	 *            第几条
	 * @param timestamp
	 *            获取报文返回时间格式
	 * @return String
	 */
	public static String getMsgTime(String msgtype, String index,
			String timestamp) {
		Date d = msg_map.get(msgtype)
				.get(Integer.parseInt(index));
		SimpleDateFormat format = new SimpleDateFormat(timestamp);
		return format.format(d);
	}

	/**
	 * 获得报文发送时间(默认返回时间格式为yyyy-MM-dd HH:mm:ss)
	 * 
	 * @param msgtype
	 *            获取报文类型(设置到容器时的key)
	 * @param index
	 *            第几条
	 * @return String
	 */
	public static String getMsgTime(String msgtype, String index) {
		Date d = msg_map.get(msgtype)
				.get(Integer.parseInt(index));
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(d);
	}

	/**
	 * 获得报文发送时间的时间戳
	 * 
	 * @param msgtype
	 *            获取报文类型(设置到容器时的key)
	 * @param index
	 *            第几条
	 * @param timeunit
	 *            0:时间戳取s 1:时间戳取ms
	 * @return long
	 */
	public static long getMsgTimeStamp(String msgtype, String index,
			String timeunit) {
		Date d = msg_map.get(msgtype)
				.get(Integer.parseInt(index));
		long timstamp = 0;
		int unit = Integer.parseInt(timeunit);
		if (unit != 0 && unit != 1) {
			Assert.fail("timeunit只能为0或者1");
		}
		if (unit == 0) {
			timstamp = d.getTime() / 1000;
		} else if (unit == 1) {
			timstamp = d.getTime();
		}
		return timstamp;
	}

	/**
	 * 获得月均里程
	 * 
	 * @param startdate 开始时间           
	 * @param enddate 结束时间
	 * @param timestamp 时间格式
	 * @param Mileage 总里程
	 * @return
	 * @throws ParseException 
	 */
	public static String getAvgMileagePerMonth(String startdate,String enddate, String timestamp, String Mileage) throws ParseException {
		Double mil = Double.parseDouble(Mileage);
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		Date now = c.getTime();
		SimpleDateFormat fomat = new SimpleDateFormat(timestamp);
		Date start = fomat.parse(startdate);
		Date end = fomat.parse(enddate);
		c.setTime(start);
		int year1 = c.get(Calendar.YEAR);
		int month1 = c.get(Calendar.MONTH);
		c.setTime(end);
		int year2 = c.get(Calendar.YEAR);
		int month2 = c.get(Calendar.MONTH);
		int tmpmonth = 0;
		if (end.before(now)) {
			if (year1==year2) {
				tmpmonth = month2 - month1 + 1;
			}
			else{
				tmpmonth = 12*(year2-year1)+month2-month1+1;
			}
		}
		else {
			if (year1==year2) {
				tmpmonth = month - month1 + 1;
			}
			else{
				tmpmonth = 12*(year-year1)+month-month1+1;
			}
		}
		DecimalFormat df = new DecimalFormat(".##");
		return df.format(mil / tmpmonth);
	}	

	/**
	 * 生成校验码-针对PHP接口
	 * 
	 * @param app_key
	 * @param method
	 * @param timestamp
	 * @param data
	 * @return String 校验码
	 */
	public String sign1(String app_key, String app_secret, String method,
			String timestamp, String data) {
		String MD5String = app_secret + "app_key" + app_key + "data" + data
				+ "method" + method + "timestamp" + timestamp + app_secret;
		return InterfaceUtils.string2MD5(MD5String);
	}

	/**
	 * 签名方法，用于生成签名。
	 * 
	 * @param jsonParams
	 *            传给服务器的参数（json）
	 * @param secret
	 *            分配给您的APP_SECRET
	 * @return String
	 */
	public static String sign2(String app_key, String method, String format,
			String timestamp, String jsonParams, String secret) {
		JSONObject jo = JSON.parseObject(jsonParams);
		Map<String, String> m = new HashMap<String, String>();
		for (Object key : jo.keySet()) {
			m.put(key.toString(), jo.get(key).toString());
		}
		m.put("app_key", app_key);
		m.put("method", method);
		m.put("format", format);
		m.put("timestamp", timestamp);
		return InterfaceUtils.sign2(m, secret);
	}
	
	public static String getStarttime() {
		return trip_starttime;
	}
	
	/**
	 * 获取1分鐘前日期字符串
	 * 
	 * @return String
	 */
	public static String GetDateString() {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MINUTE, -1);
		long date = cal.getTime().getTime();
		SimpleDateFormat matter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return matter.format(date);
	}
	/**
	 * 比较time2与time1时间间隔,精确到s
	 * 
	 * @param fstTime
	 *            time1
	 * @param sndTime
	 *            time2
	 * @param timestamp
	 *            时间格式
	 * @return
	 */
	public static long getTimeDif(String fstTime, String sndTime,
			String timestamp) {
		DateFormat df = new SimpleDateFormat(timestamp);
		long diff = 0;
		try {
			Date d1 = df.parse(fstTime);
			Date d2 = df.parse(sndTime);
			diff = Math.abs((d2.getTime() - d1.getTime()) / 1000);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return diff;
	}
	/**
	 * 获得jSONObject 值
	 * @param result
	 * @param key
	 * @return
	 */
	public static String getValue(String result, String key) {
		return JSON.parseObject(result).getString(key);
	}
	  /**
	   * 获得当前时间的前几天
	   * 
	   * @param lastInt
	   *            前几天
	   * @return String
	   */
	  public static String nowBeforeOrAfterStart(String timestamp, String interval,
	      String calendarType,int hour,int min,int sec) {
	    SimpleDateFormat sdf = new SimpleDateFormat(timestamp);
	    Calendar calender = Calendar.getInstance();
	    calender.add(Integer.parseInt(calendarType), Integer.parseInt(interval));
	    calender.set(Calendar.HOUR_OF_DAY, hour);  
	    calender.set(Calendar.MINUTE, min);   
	    calender.set(Calendar.SECOND,sec); 
	    return sdf.format(calender.getTime());
	  }
}
